#  -*- coding:utf-8 -*-
"""
@ author: 罗金盛
@ time: 2024/3/6 
@ file: 17.多进程数据共享.py

"""
import multiprocessing
import time

def push(li,lock):
    print(id(li))
    while 1:
        print('正在存放数据')
        lock.acquire()
        li.append("ljs")
        time.sleep(1)
        lock.release()

def pop(li,lock):
    print(id(li))
    while 1:
        if li:
            lock.acquire()
            print('我正在拿数据'+str(li.pop()))
            time.sleep(1)
            lock.release()

def main():
    #不是共享内存，是代理服务器操作
    li=multiprocessing.Manager().list()
    lock=multiprocessing.Manager().Lock()

    print(id(li))
    p1=multiprocessing.Process(target=push,args=(li,lock,))
    p2 = multiprocessing.Process(target=pop, args=(li,lock,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

if __name__ == '__main__':
    main()